<?php
/**
 * This Class create an Interface, which we can use to communicate with DB 
 * @author snc
 *
 */
include_once  'snc-core/env/Context.php';
class AbstractDB{
	public static $context;
	protected $tableName;
	function __construct(){
		$this->getConnection();
	}
	
	/**
	 * get connection to db	 
	 */
	public function getConnection(){
		$context = self::$context;
		$db = $context["CONFIG"]["basic"]["db"];
		$host = $db["dbHost"];
        $dbName = $db["dbName"];
		$user = $db["user"];
		$pwd = $db["pwd"];
		$conn = mysql_connect($host,$user, $pwd) or die('Error connecting to mysql');
		mysql_select_db($dbName);//test = dbname			
	}
	
	/**
	 * select query	 
	 * @param unknown_type $columns
	 * @param unknown_type $tableName
	 * @param unknown_type $where
	 * @param unknown_type $join
	 * TODO: fetch all rows and put them in to a array
	 * @return mysql_row (later will be array)
	 */
	public function select($columns,$tableName,$where=null,$join=null){
		$query = "SELECT ".$columns." FROM ".$tableName;
		if(!is_null($where) && $where != ""){
			$query .=  " WHERE " .$where;
		}
		if(!is_null($join) && $join != ""){
			$query .= " JOIN USING ".$join;
		}	
		try{		
			$result =  mysql_query($query);			
			return $this->getResult($result);
		}catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	/**
	 * insert 
	 * Enter description here ...
	 * @param unknown_type $tableName
	 * @param unknown_type $columns
	 * @param unknown_type $values
	 * @param unknown_type $where
	 */
	public function insert($tableName,$columns,$values,$where = null){
		$str_insert = "INSERT INTO ".$tableName." (".$columns.") values(".$values.")";
		try {
			$result = mysql_query($str_insert);
			return $this->getResult($result);			
		} catch (Exception $e) {
			return $e->getMessage();
		}		
	}
	
	/**
	 * delete
	 * Enter description here ...
	 * @param unknown_type $tableName
	 * @param unknown_type $where
	 */
	public function delete($tableName,$where=null){
		$str_delete = "DELETE FROM ".$tableName;
		if(!is_null($where) && $where != ""){
			$str_delete .= " WHERE ".$where;
		}
		try {
			$result = mysql_query($str_delete);
			return $this->getResult($result);
		} catch (Exception $e) {
			return $e->getMessage();
		}
	}
	
	/**
	 * update	 
	 * @param String $tableName
	 * @param array(column=>value) $data
	 * @param String $where
	 */
	public function update($tableName,$data,$where = null){
		$set = "";
		while(false !== ($elem  = current($data))){
			$set .= key($data)." = ".$elem.",";						
			next($data);
		}
		//cut the "," at the end of set
		$set = substr($set,0,strlen($set)-1);
		$str_update = "UPDATE ".$tableName. " SET ".$set;
		if(!is_null($where) && $where != ""){
			$str_update .= " WHERE ".$where;			
		}		
		try {
			$result = mysql_query($str_update);
			return $this->getResult($result);
		} catch (Exception $e) {
			return $e->getMessage();
		}
	}
	
	private function getResult($result){
		if(!$result){
			return mysql_error();
		}else{
			return $result;
		}
	}
}